package com.wsoft.service.impl;

import cn.hutool.json.JSONUtil;
import com.wsoft.core.service.RedisService;
import com.wsoft.service.IMemberCacheService;
import com.wsoft.vo.LoginUserDetail;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import static com.wsoft.constant.RedisKeyConstant.USER_INFO_CACHE;

/**
 * 功能描述
 *
 * @author: juiet
 * @date: 2024年09月03日 15:02
 */
@Service
public class MemberCacheServiceImpl implements IMemberCacheService {
    /**
     * 默认缓存时间两小时
     */
    private static long DEFAUL_TIMEOUT = 2 * 60 * 60;

    @Resource
    private RedisService redisService;

    @Override
    public void cacheMember(LoginUserDetail loginUserDetail) {
        redisService.set(USER_INFO_CACHE + loginUserDetail.getUsername(), loginUserDetail, DEFAUL_TIMEOUT);
    }

    @Override
    public LoginUserDetail getCacheUser(String username) {
        Object obj = redisService.get(USER_INFO_CACHE + username);
        if (obj != null) {
            return JSONUtil.toBean(JSONUtil.toJsonStr(obj), LoginUserDetail.class);
        }
        return null;
    }

    @Override
    public void delCacheUser(String username) {
        redisService.del(USER_INFO_CACHE + username);
    }
}
